//
//  SpecialStarDetailHeadCell.swift
//  甜言蜜语
//
//  Created by yingli_yanfa_liuxingyu on 16/8/17.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit
protocol SpecialStarDetailHeadCellDelegate {
    func callBtnSelected(text: String)
    func collectionBtnSelected(text: String)
    func collectionCompanyStyle()
}
class SpecialStarDetailHeadCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {
    var delegate:SpecialStarDetailHeadCellDelegate?
    var modelList: specialStarDetailInfo?{
        didSet{
            self.showInfo()
        }
    }
    
    @IBOutlet var thirdViewHeight: NSLayoutConstraint!//企业风貌的高度
    @IBOutlet var fourthViewHeight: NSLayoutConstraint!
    @IBOutlet var fifthViewHeight: NSLayoutConstraint!
    @IBOutlet var sixthViewHeight: NSLayoutConstraint!
    
    @IBOutlet var thirdView: UIView!
    @IBOutlet weak var fourthView: UIView!
    @IBOutlet weak var fifthView: UIView!
    @IBOutlet weak var sixthView: UIView!
    
    @IBOutlet weak var iv: UIImageView!
    @IBOutlet weak var companyName: UILabel!
    @IBOutlet weak var companyGrade: UILabel! //公司等级
    @IBOutlet weak var specifications : UILabel!//规格
    @IBOutlet weak var companyType : UILabel!
    @IBOutlet weak var address: UILabel!
    @IBOutlet weak var tel: UILabel!
    @IBOutlet weak var brief: UILabel!//简介
    @IBOutlet weak var collection: UIButton!
    
    @IBOutlet weak var collection_style: UICollectionView!// 风貌
    @IBOutlet weak var collection_theMain: UICollectionView!//主营
    @IBOutlet weak var collection_logo:UICollectionView!//品牌
    
    var thirdCellHeight = 0
    var fourthCellHeight = 0
    var fifthCellHeight = 0
    var sixthCellHeight = 0
    var cellHieight = 0
    var unitmainCount:[String]?
    var type: String! //type == 1:  企业风貌
    override func awakeFromNib() {
        super.awakeFromNib()
        
        let cellNib = UINib(nibName: "FactoryCollCell", bundle: nil)
        self.collection_style.registerNib(cellNib, forCellWithReuseIdentifier: "FactoryCollCell")
        
        let cellNib1 = UINib(nibName: "TheMainCollCell", bundle: nil)
        self.collection_theMain.registerNib(cellNib1, forCellWithReuseIdentifier: "TheMainCollCell")
        
        let cellNib2 = UINib(nibName: "TheLogoCollCell", bundle: nil)
        self.collection_logo.registerNib(cellNib2, forCellWithReuseIdentifier: "TheLogoCollCell")
        
        self.collection_logo.dataSource = self
        self.collection_logo.delegate = self
        self.collection_style.dataSource = self
        self.collection_style.delegate = self
        self.collection_theMain.dataSource = self
        self.collection_theMain.delegate = self
        self.collection_logo.scrollEnabled = false
        self.collection_theMain.scrollEnabled = false
        self.collection_style.scrollEnabled = false
        
        
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    func setCellContentWithCellInfo(cellInfo: specialStarDetailInfo?) -> Void {
        
        self.modelList = cellInfo
        self.showInfo()
        let unitstylesCount = self.modelList?.unitstyles?.count
        unitmainCount = (modelList?.unitmain.componentsSeparatedByString(","))!
        //let unitmainCount = self.modelList?.unitmain?.count
        let brandsCount = self.modelList?.brands?.count
        
        if unitstylesCount == 0{
            thirdCellHeight += 0
        }else if (unitstylesCount! % 4) > 0{
            thirdCellHeight += 30 + 100 * ((unitstylesCount! / 4) + 1)
        }else{
            thirdCellHeight += 30 + 100 * (unitstylesCount! / 4)
        }
        if unitmainCount!.count == 0{
            fourthCellHeight += 0
        }else if (unitmainCount!.count % 3) > 0{
            fourthCellHeight += 28 + 50 * ((unitmainCount!.count / 3) + 1)
        }else{
            fourthCellHeight += 28 + 50 * (unitmainCount!.count / 3)
        }
        if self.modelList?.introduction != ""{
            let font = UIFont(name: "Helvetica", size: 16)
            let height = publicMethod.getLabHeigh(self.modelList!.introduction, font: font!, width: SCREEN_WIDTH - 16)
            fifthCellHeight += 28 + Int(height)
           
        }else{
            fifthCellHeight = 0
            
        }
        if brandsCount == 0{
            sixthCellHeight += 0
           
        }else if (brandsCount! % 3 ) > 0{
            sixthCellHeight += 27 + 100 * ((brandsCount! / 3) + 1)
          
        }else{
            sixthCellHeight += 27 + 100 * (brandsCount! / 3)
           
        }
        
        self.thirdViewHeight.constant = CGFloat(Float(thirdCellHeight))
        self.fifthViewHeight.constant = CGFloat(Float(fifthCellHeight))
        self.fourthViewHeight.constant = CGFloat(Float(fourthCellHeight))
        self.sixthViewHeight.constant = CGFloat(Float(sixthCellHeight))

        
        self.thirdView.layoutIfNeeded()
        self.fourthView.layoutIfNeeded()
        self.fifthView.layoutIfNeeded()
        self.sixthView.layoutIfNeeded()
        
        cellHieight = 280 + thirdCellHeight + fourthCellHeight + fifthCellHeight + sixthCellHeight

    }
    
    //MARK:UICollectionViewDataSource
    //每一组有多少个cell
    func collectionView(collectionView:UICollectionView,numberOfItemsInSection section: Int) -> Int{
        if collectionView == self.collection_style{
            if self.modelList?.unitstyles?.count == 0 || self.modelList?.unitstyles?.count == nil{
                return 0
            }else{
                 return (self.modelList?.unitstyles?.count)!
            }
        }
        if collectionView == self.collection_theMain{
            if self.unitmainCount?.count == 0 || self.unitmainCount?.count == nil{
                return 0
            }else{
                return (self.unitmainCount?.count)!
            }
            
        }else{
            if self.modelList?.brands?.count == 0 || self.modelList?.brands?.count == nil{
                return 0
            }else{
                return (self.modelList?.brands?.count)!
            }
        }
    }
    //设置每组的cell的边界
    func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,insetForSectionAtIndex section: Int) -> UIEdgeInsets{
        if collectionView == self.collection_style{
             return UIEdgeInsetsMake(10, 10, 10, 10)//上左下右
        }
        if collectionView == self.collection_theMain{
             return UIEdgeInsetsMake(10, 10, 10, 10)//上左下右
        }else{
             return UIEdgeInsetsMake(10, 10, 10, 10)//上左下右
        }
       
    }
    //最小行间距
    func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat{
        
        if collectionView == self.collection_style{
            return 10
        }
        if collectionView == self.collection_theMain{
            return 10
        }else{
            return 10
        }

    }
    //最小列间距
    func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat{
        if collectionView == self.collection_style{
            return 10
            
        }
        if collectionView == self.collection_theMain{
            return 10
            
        }else{
            return 10
            
        }
    }
    //设定全局的Cell尺寸
    func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
        if collectionView == self.collection_style{
            return CGSizeMake(SCREEN_WIDTH/4-20,75)

        }
        if collectionView == self.collection_theMain{
            return CGSizeMake(SCREEN_WIDTH/4-20,20)

        }else{
            return CGSizeMake(SCREEN_WIDTH/4-20,75)

        }
        
       
        
    }
    //cell上显示的信息
    func collectionView(collectionView: UICollectionView,cellForItemAtIndexPath indexPath: NSIndexPath) ->UICollectionViewCell{
        if collectionView == self.collection_style{
            let model = self.modelList?.unitstyles![indexPath.item]
             let cell = self.collection_style.dequeueReusableCellWithReuseIdentifier("FactoryCollCell", forIndexPath: indexPath) as! FactoryCollCell
            cell.type = "1"
            cell.iv.sd_setImageWithURL(NSURL(string: model!.imgsrc), placeholderImage: UIImage(named: "placeholder"))
            return cell
        }
        if collectionView == self.collection_theMain{
            let  model = self.unitmainCount![indexPath.item]
            let cell = self.collection_theMain.dequeueReusableCellWithReuseIdentifier("TheMainCollCell", forIndexPath: indexPath) as! TheMainCollCell
            cell.lbl.text = model
            return cell
        }else{
            let model = self.modelList?.brands![indexPath.item]
            let cell = self.collection_logo.dequeueReusableCellWithReuseIdentifier("TheLogoCollCell", forIndexPath: indexPath) as! TheLogoCollCell
            cell.iv.sd_setImageWithURL(NSURL(string: (model?.imgsrc)!), placeholderImage: UIImage(named: "placeholder"))
            return cell
        }
        
        
    }
    
    //MARK:UICollectionViewDelegate
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
        if self.type == "1"{
            delegate?.collectionCompanyStyle()
        }
    }
    @IBAction func callBtnClicked(sender: UIButton){
        
        delegate?.callBtnSelected((self.modelList?.phone)!)
    }
    @IBAction func collectionlicked(sender: UIButton){
        delegate?.collectionBtnSelected((self.modelList?.id)!)
    }
    
    //MARK:target
    func showInfo(){
        if self.modelList?.isshoucang == true{
            self.collection.setImage(UIImage(named: "collection_selected"), forState: UIControlState.Normal)
        }else{
            self.collection.setImage(UIImage(named: "collection"), forState: UIControlState.Normal)
        }
        self.iv.sd_setImageWithURL(NSURL(string: (self.modelList?.timg)!), placeholderImage: UIImage(named: "placeholder"))
        self.companyName.text = self.modelList?.unitname
        self.companyGrade.text = self.modelList?.reputation
        self.specifications.text = self.modelList?.unitscale
        self.companyType.text = self.modelList?.unittype
        self.address.text = self.modelList?.unitaddress
        self.tel.text = self.modelList?.phone
        self.brief.text = self.modelList?.introduction
        self.collection_logo.reloadData()
        self.collection_style.reloadData()
        self.collection_theMain.reloadData()

    }
    
}
